package sw6.admin.gui;

import java.util.ArrayList;


import sw6.admin.*;
import sw6.admin.gui.logic.GuiBuilder;
import sw6.admin.gui.logic.Menu;
import sw6.admin.gui.logic.Serializer;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MenuActivity extends ListActivity {
	
	private MenuAdapter mAdapter;
	ArrayList<MenuEntry> menueEntries;
	Menu mActiveMenu;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);
		menueEntries = new ArrayList<MenuEntry>();
		mAdapter = new MenuAdapter(this, R.layout.menu_entry, menueEntries);
		setListAdapter(mAdapter);
		
		Bundle extra = getIntent().getExtras();
		byte[] bytes = extra.getByteArray("menuBytes");

		Menu menu = (Menu)Serializer.deSerializeObject(bytes, Menu.class);
		
		loadMenu(menu);
	}
	
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = mActiveMenu.getSettings().get(id).getDialog();

	    return dialog;
	}

	private void loadMenu(Menu menu) {		
		if (mActiveMenu != null) {
			mAdapter.clear();
		}
	
		mActiveMenu = menu;
		ArrayList<MenuEntry> menuEntries = new GuiBuilder().BuildMenu(menu, MenuActivity.this, (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE));
		for(MenuEntry me: menuEntries) {
			mAdapter.add(me);
		}
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		
		if (position > mActiveMenu.getSettings().size()-1) {
			int menuPos = position - mActiveMenu.getSettings().size();
			Menu menu = mActiveMenu.getMenus().get(menuPos);
			
			byte[] bytes = Serializer.serializeObject(menu);
			Intent intent = new Intent(this, MenuActivity.class);
			
			intent.putExtra("menuBytes", bytes);
			startActivity(intent);
		}
		else {
			//Dialog dialog = mActiveMenu.getSettings().get(0).getDialog();
			//dialog.show();
			showDialog(position);
		}

	  super.onListItemClick(l, v, position, id);
	}
	
	private class MenuAdapter extends ArrayAdapter<MenuEntry> {

		private ArrayList<MenuEntry> items;

		public MenuAdapter(Context context, int textViewResourceId, ArrayList<MenuEntry> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.menu_entry, null);

			MenuEntry o = items.get(position);
			TextView tt = (TextView) v.findViewById(R.id.toptext);
			TextView bt = (TextView) v.findViewById(R.id.bottomtext);
			tt.setText(o.getRealName());
			bt.setText(o.getShortDesc());
			bt.setTextSize(10);

			return v;
		}
	}
}
